package com.itheima.reggie.config;

import com.itheima.reggie.web.R;
import com.itheima.reggie.web.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @Name : GloabalExceptionHandler
 * @Author : WuQiang
 * @Date : 2022-08-11 9:42
 * @Description : 全局异常处理类
 */
@Slf4j
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class GloabalExceptionHandler {

    /**
     * 捕获业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public R catchBusinessException(BusinessException e) {
        //记录日志
        log.info("业务异常，信息如下：{}", e.getMessage());

        return R.error(e.getMessage());
    }

    /**
     * 捕获其他异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R catchException(Exception e) {
        //记录日志
        log.info("异常，信息如下：{}", e.getMessage());
        e.printStackTrace();
        return R.error(e.getMessage());
    }

}
